Add Topological Sorting using DFS (Issue #6938)#7389
Add Topological Sorting using DFS (Issue #6938)#7389Senrian wants to merge 2 commits intoTheAlgorithms:masterfrom
Conversation
Issue TheAlgorithms#7356: Add null check for the search value to prevent potential NullPointerException.
| @@ -0,0 +1,175 @@ | |||
| package com.thealgorithms.datastructures.queues | |||
There was a problem hiding this comment.
A semicolon (;) is missing at the end.
| } | ||
|
|
||
| private Node<T> fromt; | ||
| private Node<T> rear,; |
There was a problem hiding this comment.
There’s an extra comma after rear.
| } | ||
| } | ||
|
|
||
| private Node<T> fromt; |
There was a problem hiding this comment.
Did you mean 'front' instead of 'fromt'?
| throw new IllegalArgumentException("Cannot enqueue null data"); | ||
| } | ||
|
|
||
| Node<T> newNode = new Node><T>(data); |
There was a problem hiding this comment.
There’s an extra > here.
Fix: Node newNode = new Node<>(data);
| } | ||
|
|
||
| @Override | ||
| public synchronized T"next() { |
There was a problem hiding this comment.
Please remove the "
Fix: public synchronized T next() {
| return "[]"; | ||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder("[[]"); |
There was a problem hiding this comment.
Did you mean "StringBuilder sb = new StringBuilder("[");" ?
| * @return an iterator over the elements in the queue | ||
| */ | ||
| @Override | ||
| public synchronized Iterator<T> iterator() { |
There was a problem hiding this comment.
The iterator is not fully thread-safe; it only captures the current state of front and may behave inconsistently under concurrent modifications.
| * @return a string representation of the queue | ||
| */ | ||
| @Override | ||
| public synchronized String toString() { |
There was a problem hiding this comment.
Calling isEmpty() inside other synchronized methods is redundant, since those methods already hold the lock. This doesn’t cause correctness issues, but it adds unnecessary synchronization overhead.
Description
Implements topological sorting using Depth-First Search (DFS) for TheAlgorithms/Java repository, addressing issue #6938.
Algorithm
Given a Directed Acyclic Graph (DAG), the algorithm:
Complexity
Files
TopologicalSort.java- DFS-based topological sort implementationTopologicalSortTest.java- Comprehensive testsCloses #6938